package thingapi

import (
	"iot-base/common/crudextend"
	"iot-base/common/dbobj"
	"iot-base/common/webapi"

	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

type ThingEdit struct {
	dbobj.Thing
	Params    []dbobj.ThingParam `json:"params"`
	StationID string             `json:"station_id"`
	Count     int                `json:"count"`
	//ProtocolPath string             `json:"protocol_path"`
}

func initThingEditAPI(r *gin.RouterGroup) {
	mapi := r.Group("newDev")
	{
		mapi.POST("/", func(c *gin.Context) {
			var thing ThingEdit
			c.ShouldBind(&thing)
			err := EditThing(thing)
			webapi.SendResult(nil, err, c)

		})
	}
	bapi := r.Group("fromFile")
	{
		bapi.POST("/import/:mid", func(c *gin.Context) {
			mid := c.Param("mid")
			var items []map[string]interface{}
			if err := c.ShouldBindBodyWith(&items, binding.JSON); err != nil {
				webapi.SendError("station "+err.Error(), webapi.ParamMissed, c)
				return
			}
			/*var msg crudextend.StationMsg
			msg.Infos = items
			msg.Action = "import"*/
			result := crudextend.GetStationActiontResult("import", mid, "", items)
			if result != nil {
				if result.Err != nil {
					webapi.SendError(result.Err.Error(), webapi.DBError, c)
				} else {
					webapi.SendOK(c)
				}
			}

		})
	}

}
